package com.wyzz.hr.util.reflect;

import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.*;

public class ObjectProperty {

	public ObjectProperty() {
	}

	public ObjectProperty(Class objClass, String fieldName) throws Exception {
		initial(objClass, fieldName);
	}

	public void initial(Class objClass, String fieldName) throws Exception {
		if (fieldName == null || fieldName.length() < 1) {
			methods = null;
			return;
		}
		this.fieldName = fieldName;
		List methodKeyList = getInterParaForObjectProperty(objClass, fieldName);
		List methodList = (List) methodKeyList.get(0);
		methods = new Method[methodList.size()];
		int i = 0;
		for (Iterator i$ = methodList.iterator(); i$.hasNext();) {
			Method method = (Method) i$.next();
			methods[i++] = method;
		}

		keys = ((List) methodKeyList.get(1)).toArray();
		returnType = methods[methods.length - 1].getReturnType();
	}

	public Object getProperty(Object owner) {
		if (methods == null)
			return owner;
		Object property = owner;
		int i = 0;
		while (i < methods.length) {
			if (property == null)
				return null;
			try {
				if (keys[i] != null)
					property = methods[i].invoke(property,
							new Object[] { keys[i] });
				else
					property = methods[i].invoke(property, new Object[0]);
			} catch (Exception e) {
				System.out.println((new StringBuilder()).append(
						"==========error:").append(owner).append("-->").append(
						fieldName).toString());
				return null;
			}
			i++;
		}
		return property;
	}

	public Double getDouble(Object owner) throws Exception {
		Double result = new Double(0.0D);
		Object obj = getProperty(owner);
		if (obj == null)
			return result;
		String sd = obj.toString();
		if (sd.length() < 1) {
			return result;
		} else {
			Double _tmp = result;
			result = Double.valueOf(sd);
			return result;
		}
	}

	public static List getInterParaForObjectProperty(Class objClass,
			String fieldName) throws Exception {
		List result = new ArrayList();
		if (fieldName == null || fieldName.length() < 1)
			return result;
		List methodList = new ArrayList();
		List keyList = new ArrayList();
		String fieldNameArray[] = fieldName.split("\\.");
		Class objectClass = objClass;
		for (int i = 0; i < fieldNameArray.length; i++) {
			Method methodTmp;
			Object keyTmp;
			if (objectClass.getName().compareTo("java.util.List") == 0) {
				methodTmp = objectClass.getMethod("get",
						new Class[] { Integer.TYPE });
				keyTmp = new Integer(fieldNameArray[i]);
			} else if (objectClass.getName().compareTo("java.util.Map") == 0) {
				methodTmp = objectClass.getMethod("get",
						new Class[] { java.lang.Object.class });
				keyTmp = fieldNameArray[i];
			} else {
				methodTmp = objectClass.getMethod((new StringBuilder()).append(
						"get").append(
						fieldNameArray[i].substring(0, 1).toUpperCase())
						.append(fieldNameArray[i].substring(1)).toString(),
						new Class[0]);
				keyTmp = null;
			}
			methodList.add(methodTmp);
			keyList.add(keyTmp);
			objectClass = methodTmp.getReturnType();
		}

		result.add(methodList);
		result.add(keyList);
		return result;
	}

	private static Object getObjectProperty(Object owner, String fieldName)
			throws Exception {
		if (fieldName == null || fieldName.length() < 1)
			return owner;
		if (owner == null)
			return null;
		Class ownerClass = owner.getClass();
		if (ownerClass.getName().compareTo("java.util.List") == 0) {
			Integer i = new Integer(fieldName);
			List list = (List) owner;
			return list.get(i.intValue());
		} else {
			Method method = ownerClass.getMethod((new StringBuilder()).append(
					"get").append(fieldName.substring(0, 1).toUpperCase())
					.append(fieldName.substring(1)).toString(), new Class[0]);
			Object property = method.invoke(owner, new Object[0]);
			return property;
		}
	}

	public static Object getObjectFinalProperty(Object owner, String fieldName)
			throws Exception {
		if (fieldName == null || fieldName.length() < 1)
			return owner;
		String fieldNameArray[] = fieldName.split("\\.");
		if (owner == null)
			return null;
		Object property = getObjectProperty(owner, fieldNameArray[0]);
		for (int i = 1; i < fieldNameArray.length; i++)
			property = getObjectProperty(property, fieldNameArray[i]);

		return property;
	}

	public static Double getObjectDoublelValue(Object owner) throws Exception {
		Double result = new Double(0.0D);
		if (owner == null)
			return result;
		String sd = owner.toString();
		if (sd.length() < 1) {
			return result;
		} else {
			Double _tmp = result;
			result = Double.valueOf(sd);
			return result;
		}
	}

	public static double getObjectDoublelValue(Object owner,
			Method doubleValueMethode) throws Exception {
		if (owner == null)
			return 0.0D;
		else
			return ((Double) doubleValueMethode.invoke(owner, new Object[0]))
					.doubleValue();
	}

	public String getFieldName() {
		return fieldName;
	}

	public Class getReturnType() {
		return returnType;
	}

	public static final String fieldSeperator = "\\.";
	private String fieldName;
	private Class returnType;
	private Method methods[];
	private Object keys[];
}
